package com.zhudi.develop_demo_as.network;

import android.content.Context;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;


public class MyHttpClient {
    private AsyncHttpClient client;

    public MyHttpClient() {
        client = new AsyncHttpClient();
        client.setTimeout(20000);//20s
//	    client.addHeader(AppConfig.MIS_KEY, AppConfig.MIS_KEY_VALUE);
//		client.addHeader(AppConfig.ACCESS_MODE, AppConfig.ACCESS_MODE_VALUE);
//		client.addHeader(AppConfig.COMTECK_VERSION, AppConfig.COMTECK_VERSION_VALUE);
//		if (MainApplication.sessionId != null) {
//			client.addHeader(AppConfig.AUTHORIZATION, "Comteck " + MainApplication.sessionId);
//		}
    }

    /**
     * 获取AsyncHttpClient对象。
     *
     * @return
     */
    public AsyncHttpClient getAsyncHttpClient() {
        return client;
    }

    /**
     * Get请求
     *
     * @param context
     * @param url
     * @param params
     * @param responseHandler
     * @return RequestHandle
     */
    public RequestHandle get(Context context, String url, RequestParams params, ResponseHandlerInterface responseHandler) {
        return client.get(context, url, params, responseHandler);
    }

    /**
     * post请求
     *
     * @param context
     * @param url
     * @param params
     * @param responseHandler
     * @return RequestHandle
     */
    public RequestHandle post(Context context, String url, RequestParams params, ResponseHandlerInterface responseHandler) {
        return client.post(context, url, params, responseHandler);
    }

    /**
     * 取消网络请求。
     *
     * @param context
     * @param mayInterruptIfRunning
     */
    public void cancelRequests(Context context, boolean mayInterruptIfRunning) {
        client.cancelRequests(context, mayInterruptIfRunning);
    }


    /**
     * 取消所有请求。
     *
     * @param mayInterruptIfRunning
     */
    public void cancelAllRequests(boolean mayInterruptIfRunning) {
        client.cancelAllRequests(mayInterruptIfRunning);
    }

}

